package com.chs.recoginition.aip;

import feign.Response;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 自定义feign的错误处理
 *
 * @author Zongwei
 */
@Slf4j
public class FeignClientErrorDecoder implements ErrorDecoder {

    @Override
    public Exception decode(String message, Response response) {
        FeignClientError error = new FeignClientError();
        error.setHttpStatus(response.status());
        try {
            error.setErrorResponse(IOUtils.toString(response.body().asInputStream(), StandardCharsets.UTF_8));
        } catch (IOException e) {
            log.warn("从feign中读取异常response的body失败");
        }
        return new FeignClientException(message, error);
    }

}
